package com.csweimei.rentmanagementtreasure.tenant.viewModel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.Repository;
import com.csweimei.rentmanagementtreasure.http.entity.BaseResult;
import com.csweimei.rentmanagementtreasure.http.entity.BindEntEntity;
import com.csweimei.rentmanagementtreasure.http.entity.DailyListBean;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.pop.WarnPop;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.dayPaper.AddDayPaperActivity;

import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class DayPaperVM extends BaseViewModel<Repository> {


    public DayPaperVM(@NonNull Application application, Repository model) {
        super(application, model);

    }

    @Override
    public void onResume() {
        super.onResume();
        getDayData();

    }

    public ObservableField<BindEntEntity.DataDTO> entity = new ObservableField<>();

    public ObservableField<Integer> showNo = new ObservableField<>(View.VISIBLE);
    public ObservableList<DayPaperItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<DayPaperItemVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_dp);

    private void getDayData() {
        model.dailylist(AppConfig.getCurMap(model))
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<DailyListBean>() {
                    @Override
                    public void onNext(DailyListBean o) {
                        observableList.clear();
                        if (o.issucc){
                            if (o.getData().size() > 0){
                                showNo.set(View.GONE);
                                for (DailyListBean.DataDTO bean : o.getData()) {
                                    DayPaperItemVM dayPaperItemVM = new DayPaperItemVM(DayPaperVM.this);
                                    dayPaperItemVM.setData(bean);
                                    observableList.add(dayPaperItemVM);
                                }
                            }else {
                                showNo.set(View.VISIBLE);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showNo.set(View.VISIBLE);
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


    public BindingCommand back = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            finish();
        }
    });

    public BindingCommand addDay = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startActivity(AddDayPaperActivity.class);
        }
    });

    public BindingCommand exitEnt = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            WarnPop warnPop = new WarnPop(mActivity,
                    "退出企业",
                    "是否确认退出" + entity.get().getCompanyName(), new WarnPop.OnSelCallBack() {
                @Override
                public void onCallBack(String s) {
                    exitEnt();
                }
            });

            PopUtils.showPop(mActivity,warnPop,false);
        }
    });

    private void exitEnt() {
        Map<String,String> map = AppConfig.getCurMap(model);
        map.put("companyid",entity.get().getCompanyId()+"");
        model.exit_company(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult o) {
                        if (o.issucc){
                            finish();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


}
